//
//  DTAlertTextFieldView.m
//  us
//
//  Created by karry on 2025/4/14.
//

#import "DTAlertTextFieldView.h"
#import "IQKeyboardManager.h"

@interface DTAlertTextFieldView()

@property (nonatomic, assign) BOOL o_enable;
@property (nonatomic, assign) BOOL o_enableAutoToolbar;
@property (nonatomic, assign) CGFloat keyboardDistanceFromTextField;

@end

@implementation DTAlertTextFieldView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

+ (instancetype)alertContentView{
    
    DTAlertTextFieldView *view = [[DTAlertTextFieldView alloc]initWithFrame:CGRectMake(0, 0, [MOAlertBaseView alertWidth], 50 + 12 * 2)];
    return view;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupView];
    }
    return self;
}

- (void)setupView{
    
    self.o_enable = [IQKeyboardManager sharedManager].enable;
    self.o_enableAutoToolbar = [IQKeyboardManager sharedManager].enableAutoToolbar;

    // 临时修改状态
    [IQKeyboardManager sharedManager].enable = YES;
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    contentView.layer.cornerRadius = 14;
    contentView.backgroundColor = [UIColor mi_2_color];
    contentView.layer.borderWidth = 1.6f;
    contentView.layer.borderColor = UIColor.clearColor.CGColor;
    [self addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(16);
        make.right.mas_offset(-16);
        make.height.mas_equalTo(50);
        make.centerY.mas_equalTo(self);
    }];
    
    self.textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 50)];
    self.textField.layer.cornerRadius = 14;
    self.textField.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
    [self addSubview:self.textField];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.right.mas_offset(-20);
        make.top.bottom.mas_offset(0);
    }];
    self.textField.textAlignment = NSTextAlignmentCenter;
    
    [[self.textField rac_signalForControlEvents:UIControlEventEditingDidBegin]subscribeNext:^(__kindof UIControl * _Nullable x) {
        contentView.layer.borderColor = UIColor.us_blueColor.CGColor;
    }];
    
    [[self.textField rac_signalForControlEvents:UIControlEventEditingDidEnd]subscribeNext:^(__kindof UIControl * _Nullable x) {
        contentView.layer.borderColor = UIColor.clearColor.CGColor;
    }];
    

}

- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];

    if (newSuperview) {
        // 保存原状态
        self.o_enable = [IQKeyboardManager sharedManager].enable;
        self.o_enableAutoToolbar = [IQKeyboardManager sharedManager].enableAutoToolbar;
        self.keyboardDistanceFromTextField = [IQKeyboardManager sharedManager].keyboardDistanceFromTextField;

        // 临时修改状态
        [IQKeyboardManager sharedManager].enable = YES;
        [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
        [IQKeyboardManager sharedManager].keyboardDistanceFromTextField = 100;

    } else {
        // 离开时不用处理，在 didMoveToSuperview 中统一恢复
    }
}

- (void)didMoveToSuperview {
    [super didMoveToSuperview];
    if (!self.superview) {
        // 恢复原状态
        [IQKeyboardManager sharedManager].enable = self.o_enable;
        [IQKeyboardManager sharedManager].enableAutoToolbar = self.o_enableAutoToolbar;
        [IQKeyboardManager sharedManager].keyboardDistanceFromTextField = self.keyboardDistanceFromTextField;
    }
}

@end
